﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;

namespace XrmLibrary.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Converts the String to a SecureString.
        /// </summary>
        /// <param name="toSecureString">The string value to convert.</param>
        /// <returns>The converted SecureString.</returns>
        public static SecureString ToSecureString(this string toSecureString)
        {
            char[] stringArray = toSecureString.ToCharArray();
            SecureString secureString = new SecureString();
            foreach (char c in stringArray)
            {
                secureString.AppendChar(c);
            }
            return secureString;
        }

        /// <summary>
        /// Strips all line breaks from the String.
        /// </summary>
        /// <param name="toSingleLine">The string to convert to single line.</param>
        /// <returns>The single line formatted string.</returns>
        public static string StripLineBreaks(this string toSingleLine)
        {
            return toSingleLine.Replace(Environment.NewLine, string.Empty).Replace('\n', '\t');
        }

        /// <summary>
        /// Formats the String as a phone number.
        /// </summary>
        /// <param name="phoneNumber">The phone number.</param>
        /// <returns>The formatted phone number.</returns>
        public static string FormatPhoneNumbers(this string phoneNumber)
        {
            string finalPhoneNumber = Regex.Replace(phoneNumber, "[^0-9]", "");


            if (finalPhoneNumber.Length == 10)
            {
                finalPhoneNumber = String.Format("{0:(###)###-####}", double.Parse(finalPhoneNumber));
            }
            else
            {
                // If the number isn't a valid, return the original
                return phoneNumber;
            }

            return finalPhoneNumber;
        }

        /// <summary>
        /// Capitalizes all words in the specified String.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The titleized input.</returns>
        public static string Titleize(this string input)
        {
            if (input.Length != 0)
            {
                return Regex.Replace(input.ToLower(), @"(?<space>\s*)(?<first>.)(?<rest>\S*)", new MatchEvaluator(OnMatch));
            }
            return input;
        }

        private static string OnMatch(Match match)
        {
            return string.Format("{0}{1}{2}", match.Groups["space"].Value, match.Groups["first"].Value.ToUpper(), match.Groups["rest"].Value);
        }

        public static String ToCsv(this IEnumerable<String> values)
        {
            StringBuilder csv = new StringBuilder();
            foreach(String value in values)
            {
                csv.AppendFormat("'{0}',", value);
            }
            return csv.ToString();
        }
    }
}
